from util.PATH import PATH as PATH

class Dataset():
	def __init__(self, dataset_name):
		self.PATH = PATH(dataset_name)

	def get_features_and_labels(self, developers_list, bugs_msg_all, datas_ids):
		'''
		根据传进来的bugid，提取对应bug报告的文本特征和标记序列
		:param developers_list: 
		:param bugs_msg_all: 
		:param datas_ids: 
		:return: 
		'''
		features = []
		labels = []
		for index in range(len(datas_ids)):
			name = datas_ids[index]  # 取出index位置的bug_id, 作为文件名
			feature = []
			with open(self.PATH.path_corpus + str(name), 'r') as reader:
				for line in reader.readlines():
					feature.append(line.strip())
			features.append(' '.join(feature))
			label = bugs_msg_all[name][0]  # assign_to
			try:
				label_id = developers_list.index(label)
			except ValueError:
				pass
			labels.append(label_id)
		return features, labels

	def get_index_of_features_and_labels(self, vocabulary, developers_list, bugs_msg_all, datas_ids):
		features = []
		labels = []
		for index in range(len(datas_ids)):
			name = datas_ids[index]  # 取出index位置的bug_id, 作为文件名
			feature = []
			with open(self.PATH.path_corpus + str(name), 'r') as reader:
				for line in reader.readlines():
					feature.append(vocabulary.index(line.strip()))
			if len(feature) < 200:
				feature += [0]*(200-len(feature))
			else:
				feature = feature[:200]
			features.append(feature)

			label = bugs_msg_all[name][0]  # assign_to
			try:
				label_id = developers_list.index(label)
			except ValueError:
				pass
			labels.append(label_id)
		return features, labels